package com.xuln.common.category.action;

import java.util.List;

import javax.annotation.Resource;

import com.xuln.common.category.entity.Category;
import com.xuln.common.category.entity.CategoryType;
import com.xuln.common.category.form.CategoryForm;
import com.xuln.common.category.service.ICategoryService;
import com.xuln.framework.action.BaseAction;
import com.xuln.framework.entity.PrimaryKey;

public class CategoryAction extends BaseAction {
	private static final long serialVersionUID = -6714115653589359271L;

	private CategoryForm categoryForm;
	@Resource
	private ICategoryService categoryService;

	public String getArticleCategory() throws Exception {
		List<Category> categorys = categoryService.getAllCategoryByType(CategoryType.ATRICLE.toString(),
				getUserLocale());
		categoryForm.setChilds(categorys);
		return "getArticleCategory";
	}
	
	public String getLeftCategoryMenu() throws Exception {
		categoryInfo();
		return "leftCategoryMenu";
	}

	public String categoryInfo() throws Exception {
		Category category = categoryService.getCategory(categoryForm.getId(), getUserLocale());
		List<Category> allParents = categoryService.getAllParent(categoryForm.getId(), getUserLocale());
		List<Category> allChilds = categoryService.getAllChildCategory(categoryForm.getId(), getUserLocale());
		categoryForm.setCategory(category);
		categoryForm.setParents(allParents);
		categoryForm.setChilds(allChilds);
		return "categoryInfo";
	}

	public String addCategory() throws Exception {
		Category category = categoryForm.copy();
		category.setPK(new PrimaryKey(getUserLocale()));
		categoryService.save(category);
		categoryForm.setId(category.getPK().getId());
		return categoryInfo();
	}

	public String updateCategory() throws Exception {
		Category category = categoryForm.copy();
		category.setPK(new PrimaryKey(categoryForm.getId(), getUserLocale()));
		categoryService.update(category);
		return categoryInfo();
	}

	public String categoryEdit() throws Exception {
		categoryInfo();
		return "categoryEdit";
	}

	public CategoryForm getCategoryForm() {
		return categoryForm;
	}

	public void setCategoryForm(CategoryForm categoryForm) {
		this.categoryForm = categoryForm;
	}
}
